﻿using Payroll.Biz.Legacy.DAL;
using Payroll.Biz.Legacy.Models;
using Payroll.Biz.Legacy.ServiceParams;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Payroll.Biz.Legacy.Services
{
    public class AddServiceChargeService : AbstractService, IService<AddServiceChargeServiceParams>
    {
        public AddServiceChargeService(IPayrollDatabase database) : base(database)
        {
        }

        public ServiceResult Execute(AddServiceChargeServiceParams serviceParams)
        {
            Employee employee = Database.GetEmployee(serviceParams.EmployeeId);
            if (employee == null)
            {
                throw CreateServiceException("No such employee.");
            }

            if (!(employee.Affiliation is UnionAffiliation))
            {
                throw CreateServiceException("Tried to add service charge to union memeber without a union affiliation");
            }

            Guid id = Guid.NewGuid();

            ServiceCharge serviceCharge = new ServiceCharge(id, employee.Id, serviceParams.Time, serviceParams.Amount);

            Database.AddServiceCharge(serviceCharge);

            return ServiceResult.CreateSuccessResult(serviceCharge.Id);
        }
    }
}
